package 从头到尾打印数组;

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        ListNode head = new ListNode(1);
        ListNode head1 = new ListNode(2);
        ListNode head2 = new ListNode(3);

        head.next = head1;
        head1.next = head2;
        solution.reversePrint(head);
    }

    public int[] reversePrint(ListNode head) {
        ArrayList<Integer> list = new ArrayList();
        if(head != null){
            list.add(head.val);
        }

        while(head != null && head.next != null){
            head = head.next;
            list.add(head.val);
        }

        int length = list.size();
        int[] t = new int[length];

        for (int i = 0; i < length; i++) {
            t[i] = list.get(length -1 -i);
        }
        return t;
    }

    public static class ListNode {
      int val;
      ListNode next;
      ListNode(int x) { val = x; }
     }
}
